<script setup name="Detail">
import {ref, reactive, toRefs, getCurrentInstance, onMounted, nextTick} from 'vue'
import {checkMaterial, listDetail, getDetail, delDetail, addDetail, updateDetail} from "@/api/InventoryCheckDetail";

import {useRoute} from 'vue-router';
import {Refresh, Search} from "@element-plus/icons-vue";


const route = useRoute()
const {proxy} = getCurrentInstance();
const detailRef = ref();
const detailList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    checkId: null,
    assetId: null,
    foundLocationId: null,
    locationId: null,
    systemQuantity: null,
    actualQuantity: null,
    status: null,
    difference: null,
    batchNo: null,
    discrepancyNote: null
  },
  rules: {
    checkId: [
      {required: true, message: "盘点单ID不能为空", trigger: "blur"}
    ],
    assetId: [
      {required: true, message: "资产ID不能为空", trigger: "blur"}
    ],
    systemQuantity: [
      {required: true, message: "系统数量不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询盘点单明细列表 */
function getList() {
  loading.value = true;
  checkMaterial(queryParams.value.checkId).then(response => {
    detailList.value = response.data;
    console.log("getList", detailList.value)
    // total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    inventoryCheckDetailId: null,
    checkId: null,
    assetId: null,
    foundLocationId: null,
    locationId: null,
    systemQuantity: null,
    actualQuantity: null,
    status: null,
    difference: null,
    batchNo: null,
    discrepancyNote: null
  };
  proxy.$refs["detailRef"].resetFields();
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.inventoryCheckDetailId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加盘点单明细";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  open.value = true;

  const _inventoryCheckDetailId = row.inventoryCheckDetailId || ids.value

  nextTick(() => {
    getDetail(_inventoryCheckDetailId).then(response => {
      reset();
      form.value = response.data;

      title.value = "修改盘点单明细";
    });
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["detailRef"].validate(valid => {
    if (valid) {
      if (form.value.inventoryCheckDetailId != null) {
        console.log(form.value)
        updateDetail(form.value).then(res => {
          console.log(res)
          ElMessage.success("修改成功")
          open.value = false;
          getList();
        });
      } else {
        addDetail(form.value).then(response => {
          ElMessage.success("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _inventoryCheckDetailIds = row.inventoryCheckDetailId || ids.value;
  proxy.$modal.confirm('是否确认删除盘点单明细编号为"' + _inventoryCheckDetailIds + '"的数据项？').then(function () {
    return delDetail(_inventoryCheckDetailIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('check/detail/export', {
    ...queryParams.value
  }, `detail_${new Date().getTime()}.xlsx`)
}


onMounted(() => {
  console.log('接收到的盘点单route:', route)
  const checkId = route.query.id
  console.log('接收到的盘点单ID:', checkId)
  queryParams.value.checkId = checkId
  getList();

  // 可以在这里调用 getList() 并传入 checkId 过滤数据
})
</script>
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="实际位置" prop="foundLocationId">
        <el-input
          v-model="queryParams.foundLocationId"
          placeholder="请输入实际位置ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="位置" prop="locationId">
        <el-input
          v-model="queryParams.locationId"
          placeholder="请输入位置ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="差异数量" prop="difference">
        <el-input
          v-model="queryParams.difference"
          placeholder="请输入差异数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
        <el-button type="primary" plain :icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--            type="success"-->
    <!--            plain-->
    <!--            icon="Edit"-->
    <!--            :disabled="single"-->
    <!--            @click="handleUpdate"-->
    <!--            v-hasPermi="['check:detail:edit']"-->
    <!--        >修改-->
    <!--        </el-button>-->
    <!--      </el-col>-->
    <!--      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <el-table v-loading="loading" table-layout="auto" :data="detailList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column prop="code" align="center" label="耗材编码"/>
      <el-table-column prop="name" align="center" label="名称"/>
      <el-table-column prop="warehouseName" align="center" label="仓库位置"/>
      <el-table-column prop="specification" align="center" label="规格型号"/>
      <el-table-column prop="unit" label="单位" align="center"/>
      <el-table-column prop="categoryName" label="耗材类别" align="center"/>
      <el-table-column prop="quantity" label="库存" align="center"/>
      <el-table-column prop="maxStock" label="最高库存" align="center"/>
      <el-table-column prop="minStock" label="最低库存" align="center"/>
      <!-- <el-table-column label="实际位置" align="center" prop="foundLocationId" /> -->
      <el-table-column label="实际数量" align="center" prop="actualQuantity" />
       <!-- 0-闲置 1-使用中 2-维修中 3-已报废 4-已处置 -->
      <!-- <el-table-column label="实际状态" align="center" prop="actualStatus" /> -->
      <el-table-column label="差异数量" align="center" prop="difference"/>
      <el-table-column label="差异说明" align="center" prop="discrepancyNote" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['check:detail:edit']">修改</el-button>
          <!-- <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['check:detail:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改盘点单明细对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="detailRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="盘点单ID" prop="checkId" v-show="false">
          <el-input v-model="form.checkId" placeholder="请输入盘点单ID" />
        </el-form-item>
        <el-form-item label="资产ID" prop="assetId" v-show="false">
          <el-input v-model="form.assetId" placeholder="请输入资产ID" />
        </el-form-item>
        <el-form-item label="实际位置ID" prop="foundLocationId" v-show="false">
          <el-input v-model="form.foundLocationId" placeholder="请输入实际位置ID" />
        </el-form-item>
        <el-form-item label="位置ID" prop="locationId" v-show="false">
          <el-input v-model="form.locationId" placeholder="请输入位置ID" />
        </el-form-item>
        <el-form-item label="差异数量" prop="difference">
          <el-input v-model="form.difference" placeholder="请输入差异数量" />
        </el-form-item>
        <el-form-item label="差异说明" prop="discrepancyNote">
          <el-input v-model="form.discrepancyNote" placeholder="请输入差异说明" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>


